package com.yq.tanhua.ui.chat.conversationlist

import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.fmt.mvi.learn.gobal.log
import com.hyphenate.chat.EMConversation
import kotlinx.coroutines.flow.MutableStateFlow


@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun ConversationListView(
    onClick: ((conversation: EMConversation) -> Unit)? = null,
    conversationViewUIData: MutableStateFlow<ConversationViewUIData>
) {
    "ConversationListView ".log()
    val datas = conversationViewUIData.value
    if (datas.conversationDatas == null || datas.conversationDatas?.size == 0) {
        Box(
            Modifier
                .fillMaxHeight()
                .fillMaxWidth(),
            contentAlignment = Alignment.Center
        ) {
            Text("暂无会话数据")
        }
    } else {
        LazyColumn(content = {
            datas.conversationDatas!!.forEach {
                item {

                    Row(
                        modifier = Modifier.padding(20.dp).clickable {
                            onClick?.invoke(it.value)

                        },
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        AsyncImage(
                            model = "",
                            contentDescription = "",
                            modifier = Modifier
                                .size(48.dp)
                                .clip(
                                    CircleShape
                                )
                                .background(Color.Gray)
                        )
                        Column(
                            Modifier
                                .padding(horizontal = 10.dp)
                                .weight(1f)
                        ) {

                            Text(text = it.value.conversationId())
                            Text(text = "点击查看消息")
                        }
                        Box(
                            modifier = Modifier
                                .size(15.dp)
                                .clip(
                                    CircleShape
                                )
                                .background(Color.Red)
                                .padding(0.dp)
                        ) {
                            Text(
                                text = it.value.unreadMsgCount.toString(),
                                style = TextStyle(color = Color.White),
                                fontSize = 8.sp,
                                modifier = Modifier.align(Alignment.Center)
                            )
                        }

                    }
                }
            }
        })
    }
}